package com.tlias.controller;


import com.tlias.jwt.annotation.LoginToken;
import com.tlias.pojo.Result;
import com.tlias.utils.AliossUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;


@Slf4j
@RestController
public class UploadController {
    @Autowired
    private AliossUtils aliossUtils;

    /**
     * 图片文件上传
     * @param image 客户端图片地址
     * @return ali url
     */
    @LoginToken
    @PostMapping("/upload")
    public Result upload(MultipartFile image) {
        log.info("upload -> 请求参数：image[{}]", image);
        //使用AliOss工具类，实现文件存储
        try {
            //图片上传到ali云
            String url = aliossUtils.upload(image);
            log.info("aliurl[{}]" + url);
            return Result.success(url);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.error("上传失败");
        }
    }
}
